/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import com.mysql.jdbc.Statement;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author Jose
 */
@WebServlet(name = "Verifica_Login", urlPatterns = {"/Verifica_Login"})
public class Verifica_Login extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try 
        {
            Conexion_DB conector = new Conexion_DB();
            
            /*Se obtienen el nombre y password del usuario*/
            String alias = request.getParameter("usuario");
            String clave = request.getParameter("clave");
         
            /*Variables utilizadas para verificar datos de Login*/
            String aux_alias = null;
            String aux_clave = null;
            int lector = 1;
            int publicador = 2;
            int administrador = 3;
            int super_admin = 4;
            int aux_tipo = 0;
            
            boolean user_l = false;
            boolean user_p = false;
            boolean user_a = false;
            boolean user_s = false;
            
            try
            {
               conector.Conectar();
               String strquery = "SELECT * from usuario u";
               Statement st = (Statement) conector.GetConexion().createStatement();
               ResultSet rs = st.executeQuery(strquery);
               
               while(rs.next())
               {
                   aux_alias = rs.getString("u.u_alias");
                   aux_clave = rs.getString("u.u_password");
                   aux_tipo = rs.getInt("u.u_tipousuario");
                   
                   /*Verificacon para determinar si el usuario pertenece a la DB*/
                   if(alias.equals(aux_alias) && clave.equals(aux_clave) && aux_tipo == lector)
                   {
                       user_l = true;
                   }
                   else if(alias.equals(aux_alias) && clave.equals(aux_clave) && aux_tipo == publicador)
                   {
                       user_p = true;
                   }
                   else if(alias.equals(aux_alias) && clave.equals(aux_clave) && aux_tipo == administrador)
                   {
                       user_a = true;
                   }
                   else if(alias.equals(aux_alias) && clave.equals(aux_clave) && aux_tipo == super_admin)
                   {
                       user_s = true;
                   }
               }
               
               /*Validando credenciales para dar permisos*/
               
               if(user_l == true)
               {
                   response.sendRedirect("Lector.jsp");
               }
               else if(user_p == true)
               {
                   response.sendRedirect("Publicador.jsp");
               }
               else if(user_a == true)
               {
                   response.sendRedirect("Administrador.jsp");
               }
               else if(user_s == true)
               {
                   response.sendRedirect("Super_Administrador.jsp");
               }         
               else
               {
                   response.sendRedirect("index.jsp");
                   JOptionPane.showMessageDialog(null, "Error credenciales incorrectas");               
               }              
            }
            catch (Exception e)
            {
                
            }
        } 
        finally 
        {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
